<?php
	session_start();
	
	include '../../../php/globalVars.php';
	include '../../../php/globalFuncs.php';
	include '../../../php/connections/dbconnect.php';
?>
<style>
	#messageInboxHeader {
		border-bottom: 1px solid #E2E2E2;
		padding-bottom: 5px;
		width: 100%;
	}
	
	#inboxItemHolder {
		height: 240px;
		overflow-x: hidden;
		overflow-y: auto;
	}
	
	#inboxItemHolder div.messageItem {
		padding: 7px;
		overflow: hidden;
		border-bottom: 1px solid #EBEBEB;
	}
	
	#inboxItemHolder div.messageItem div.nameSubject {
		float:left;
		margin-top:1px;
		width:250px;
		overflow:hidden;
	}
	
	#inboxItemHolder div.messageItem img.profileThumb {
		margin-right: 5px;
	}
	
	#inboxItemHolder div.messageItem a {
		text-decoration: none;
	}
	
	#inboxItemHolder div.messageItem:hover {
		background-color: #D8DFEA;
		cursor: pointer;
	}
	
	#loadMoreInboxItems {
		text-align: center;
		background-color: #EDEFF4;
		height: 30px;
		margin: 5px auto 0 auto;
		font-weight: bold;
		color: #4486C7;
		width: 280px;
		line-height: 30px;
	}
	
	#loadMoreInboxItems:hover {
		text-align: center;
		background-color: #D8DFEA;
		cursor: pointer;
	}
</style>
<script>
	$("#createNewMessage").click(function(){
		$("#messageSendingToPost").val("");
		$("#messageTextPost").val("");
		$("#messageSendingError").val("");
		
		
		Popup("Messages | New message", "messages/createNewMessage", 0);
	});
	
	$(".messageItem").click(function(){
		var threadID = $(this).attr('id');
		Popup("Messages | " + 'Read Messages', "messages/readMessage", 0);
	});
</script>

<table id="messageInboxHeader">
	<tr>
		<td><a href='javascript:void(0);' id='createNewMessage'><b>New message</b></a></td><td align='right'>Search: <input type='text' /></td>
	</tr>
</table>

<div id="inboxItemHolder">
	<?php 
		$inboxQuery = mysql_query("
		SELECT MT.ID AS 'threadID',
			(SELECT MAX(M.dateSent) FROM Messages AS M WHERE M.messageThreadID = MT.ID) AS 'dateSent',
			(SELECT M.message FROM Messages AS M WHERE MT.ID = M.messageThreadID ORDER BY M.ID DESC LIMIT 1) AS  'message',
			(SELECT M.fromID FROM Messages AS M WHERE MT.ID = M.messageThreadID ORDER BY M.ID DESC LIMIT 1) AS  'lastMessageFrom'
		FROM MessageThread AS MT
			INNER JOIN MessageThreadSubscribers AS MTS
			ON MT.ID = MTS.messageThreadID
		WHERE MTS.userID = '{$userID}'
		ORDER BY dateSent DESC
		");
			while($row = mysql_fetch_assoc($inboxQuery)){
			
			$threadID = $row['threadID'];
			$lastMessageFrom = $row['lastMessageFrom'];
			$message = strlen($row['message']) > 45 ? trim(substr($row['message'], 0, 45)).'...' : $row['message'];
			$dateSent = $row['dateSent'];
			
				$usersInThreadQuery = mysql_query("SELECT `userID` FROM `MessageThreadSubscribers` WHERE `messageThreadID` = '{$threadID}'");
			
				$usersInThread = "";
				while($row2 = mysql_fetch_assoc($usersInThreadQuery)){
					$usersInThread .= DisplayName($row2['userID']).', ';
				}
				
				$usersInThread = trim(str_replace(DisplayName($userID).', ', '', $usersInThread));
				$usersInThread = substr($usersInThread, 0, -1);
				
				if(mysql_num_rows($usersInThreadQuery) > 3){
					
					$totalOtherUsersInThread = mysql_num_rows($usersInThreadQuery) - 3; // minus me and two already displayed
					
					if($totalOtherUsersInThread == 1){
						$totalOtherUsersInThread .= ' other';
					} else {
						$totalOtherUsersInThread .= ' others';
					}
				
					$usersInThreadArray = explode(', ', $usersInThread);
					$usersInThread = $usersInThreadArray[0].', '.$usersInThreadArray[1].', and '.$totalOtherUsersInThread;
				}
			
				if($lastMessageFrom == $userID){
					$message = "<img src='/assets/images/website/main/messageIsent.png' align='left'/> ".$message;
				}	
	?>
			<div class='messageItem' id="<?php echo $threadID; ?>">
				<img src='/assets/images/profilePics/s/noProfilePic.png' align='left' class='profileThumb'/>
				<div class='nameSubject'>
					<a href='javascript:void(0);'><?php echo $usersInThread; ?></a><br />
					<?php echo $message; ?>
				</div>
				<div style='float:right;color:#999999'>
					<i><?php echo time_stamp(strtotime($dateSent)); ?></i>
				</div>
			</div>

	<?php
			}
	?>
</div>
